/**
 *  MASK css
 * .loadmask { z-index: 999; position: absolute; top:0; left:0; opacity: 0.7; background-color: #000; width: 100%; height: 100%; }
 * .loadmask-msg { z-index: 20001; position: absolute; padding:10px; margin: 0 auto; display: inline-block; border: 1px solid #ccc; border-color: rgba(255,255,255,0.5); background: #333; background: rgba(0,0,0,0.5); -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px; -webkit-background-clip: padding-box; -moz-box-shadow: 0 2px 4px 4px rgba(0,0,0,0.2); -webkit-box-shadow: 0 2px 4px 4px rgba(0,0,0,0.2); box-shadow: 0 2px 4px 4px rgba(0,0,0,0.2);}
 * .loadmask-msg div { padding: 15px 10px 15px 60px; background: url('../img/loading.gif') no-repeat 0 0; color:#fff; cursor:wait; font: normal 16px Tahoma, helvetica, sans-serif;  text-shadow: 1px 1px 1px #000; -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px;}
 * 
 * .masked { overflow: hidden !important; }
 * .masked-relative { position: relative !important; }
 * .masked-hidden { visibility: hidden !important; }
 * 
 * 
 * Shows loading mask over selected elements
 *
 * @param string label		Text message to display over the mask. If not provided only mask will be displayed  	
 * @param int delay			A number of milliseconds to delay showing the mask (if action is completed before that - mask won't show up)
 * @param int fadein		if exists - add fadeIn animation to the mask
 */

;(function($){
	$.maskElement = function(el, label, fadein){
		//if (!el.is(':visible')) return;
		if (el.data('_mask_timeout') !== undefined){																			// if this element has delayed mask scheduled then remove it and display the new one 
			clearTimeout(el.data('_mask_timeout')); 
			el.removeData('_mask_timeout'); 
		}
		var offset, L, T, W, H, t = +new Date(), isHidden = false, oldDisp ='';
		
		if (!el.is(':visible')){ isHidden = true; oldDisp = el.css('display'); el.css('display', 'block'); }					// if el is hidden - show temporarily to get offset
		
		offset = el.position();
		L = offset.left;
		T = offset.top;
		W = el.innerWidth();
		H = el.innerHeight();

		if (isHidden) el.css('display', oldDisp);																				// if it was hidden - hide back
		
		if(!el.hasClass('masked')){																								// element is not masked - mask it
			if(el.css('position') == 'static') el.addClass('masked-relative');
			el.addClass('masked');
			
			var zIdx = ((+el.css('z-index'))+1)||500, 
				maskDiv = $('<div id="mask_'+t+'" class="loadmask"></div>'); 
			
			maskDiv.css({ top: T, left: L, height: H, width: W, zIndex: Math.max(zIdx,500) });

			if (navigator.userAgent.toLowerCase().indexOf('msie') > -1){														// auto height fix for IE 
				maskDiv
					.height(el.height() + parseInt(el.css('padding-top')) + parseInt(el.css('padding-bottom')))
					.width(el.width() + parseInt(el.css('padding-left')) + parseInt(el.css('padding-right')));
			}
			el.data('maskT', t).parent().append(maskDiv);
			if (fadein) maskDiv.fadeTo(0,0).clearQueue().stop().fadeTo(fadein,.4);
		}
		
		if (label){																												// element is already masked - just update label
			t = el.data('maskT'); 
			var msgHtml, mT, mL, maskMsgDiv = $('#maskmsg_' + t);
			
			msgHtml = '<div id="maskmsg_' + t + '" class="loadmask-msg" style="display:none;"><div>' + label + '</div></div>';

			if (maskMsgDiv.length) maskMsgDiv.find('div').html(label);
			else maskMsgDiv = $(msgHtml).appendTo(el.parent());

			mT = (H - maskMsgDiv.outerHeight()) / 2 - 5,
			mL = (W - maskMsgDiv.outerWidth()) / 2;

			maskMsgDiv.css({ top: T, left: L, marginTop: mT, marginLeft: mL }).show();											// center & show
			$('#mask_'+t).css({ top: T, left: L, height: H, width: W });
		}	
	};
	$.unmaskElement = function(el, speed, cb){ 
		var t = el.data('maskT'); 
		if (el.data('_mask_timeout') !== undefined){																			// if this element has delayed mask scheduled then remove it 
			clearTimeout(el.data('_mask_timeout')); 
			el.removeData(['_mask_timeout', 'maskT']); 
		}
		$('#maskmsg_'+t+',#mask_'+t).clearQueue().stop().fadeOut(speed||500, function(){ $(this).remove(); if(cb&&typeof cb==='function') cb.call(this); });
		el.removeClass('masked masked-relative').find('select').removeClass('masked-hidden');
	};
	$.fn.mask = function(label, delay, fadein){ 
		return $(this).each(function(){ 
			var el=$(this); 
			el.data('_mask_timeout', setTimeout(function(){ $.maskElement(el, label, fadein); }, delay||0));
		}); 
	};
	$.fn.unmask = function(speed, callback){ 
		return $(this).each(function(){ 
			$.unmaskElement($(this), speed, callback); 
		}); 
	};
})(jQuery);